package com.service.sys;

import com.common.ErrorCodeEnum;
import com.model.base.BaseEntity;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.dao.primary.sys.SysMenuMapper;
import com.github.pagehelper.PageHelper;
import com.model.pojo.sys.SysMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: zql
 * @Description: 菜单权限服务
 * @Data: 2021-07-09
 */
@Slf4j
@Service
public class SysMenuService {

    @Resource
    SysMenuMapper sysMenuMapper;

    /**
     * @Author: zql
     * @Description: 创建菜单权限
     * @param sysMenu 被创建对象主体
     * @Data: 2021-07-09
     */
    public void create(SysMenu sysMenu) {
        String name = SecurityUtils.getUsername();
        sysMenu.setCreateTime(null);
        sysMenu.setUpdateTime(null);
        sysMenu.setCreator(name);
        sysMenu.setModifier(name);
        sysMenuMapper.insertSelective(sysMenu);
    }

    /**
     * @Author: zql
     * @Description: 创建菜单权限列表
     * @param sysMenuList 增加的对象主体列表
     * @Data: 2021-07-09
     */
    public void createList(List<SysMenu> sysMenuList) {
        String name = SecurityUtils.getUsername();
        sysMenuList.forEach(o -> {
            o.setCreator(name);
            o.setModifier(name);
            o.setUpdateTime(null);
            o.setCreateTime(null);
        });
        sysMenuMapper.insertListSelective(sysMenuList);
    }

    /**
     * @Author: zql
     * @Description: 更新菜单权限信息
     * @param sysMenu 更新的对象主体内容
     * @Data: 2021-07-09
     */
    public void update(SysMenu sysMenu) {
        String name = SecurityUtils.getUsername();
        sysMenu.setCreateTime(null);
        sysMenu.setUpdateTime(null);
        sysMenu.setCreator(null);
        sysMenu.setModifier(name);
        sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
    }

    /**
     * @Author: zql
     * @Description: 更新菜单权限列表信息
     * @param sysMenuList 更新的对象主体内容列表
     * @Data: 2021-07-09
     */
    public void updateList(List<SysMenu> sysMenuList) {
        String name = SecurityUtils.getUsername();
        sysMenuList.forEach(o -> {
            o.setCreator(name);
            o.setModifier(name);
            o.setUpdateTime(null);
            o.setCreateTime(null);
        });
        sysMenuMapper.updateListSelective(sysMenuList);
    }

    /**
     * @Author: zql
     * @Description: 更新或创建
     * @param sysMenuList 更新或创建的对象主体内容列表
     * @Data: 2021-07-09
     */
    public void updateAndInsert(List<SysMenu> sysMenuList) {
        List<SysMenu> updateList = new ArrayList<>();
        List<SysMenu> createList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList){
            if (StringUtils.isNotNull(sysMenu.getSysMenuId()) &&
                    sysMenuMapper.has(sysMenu.getSysMenuId(),null) >0){
                updateList.add(sysMenu);
            } else {
                createList.add(sysMenu);
            }

        }
        if (updateList.size() > 0) {
            updateList(updateList);
        }
        if (createList.size() > 0) {
            createList(createList);
        }
    }

    /**
     * @Author: zql
     * @Description: 查看菜单权限列表
     * @param baseEntity 基础类
     * @Data: 15:02 2021/6/3
     */
    public List<SysMenu> getList(BaseEntity baseEntity) {
        PageHelper.clearPage();
        PageHelper.startPage(baseEntity.getPage(), baseEntity.getPageSize(), baseEntity.getSortRow() + baseEntity.getSortOrder());
        return sysMenuMapper.selectList(baseEntity.getIsDelete());
    }

    public List<SysMenu> getList() {
        PageHelper.clearPage();
        return sysMenuMapper.selectList(DataStatus.OK.getCode());
    }

    /**
     * @Author: zql
     * @Description: 查看菜单权限
     * @param sysMenuId 菜单权限Id
     * @Data: 2021-07-09
     */
    public SysMenu get(Long sysMenuId, Byte isDelete) {
        SysMenu sysMenu = sysMenuMapper.selectByPrimaryKey(sysMenuId, isDelete);
        if (StringUtils.isNull(sysMenu)) {
            throw new BusinessException(ErrorCodeEnum.DATA_NOT_FOUNT);
        }
        return sysMenu;
    }

    public SysMenu get(Long sysMenuId) {
        SysMenu sysMenu = sysMenuMapper.selectByPrimaryKey(sysMenuId, null);
        return sysMenu;
    }

    /**
     * @Author: zql
     * @Description: 对象校验
     * @param sysMenu 校验的对象
     * @param toUpdate 确定对象是更新还是创建
     * @Data: 2021-07-09
     */
    public void verifyInfo(SysMenu sysMenu, boolean toUpdate) {
        //boolean toInsert = !toUpdate;
        if (StringUtils.isNotNull(sysMenu.getIsDelete())) {
            if (!DataStatus.contains(sysMenu.getIsDelete())) {
                log.error("数据状态设置错误:{}", sysMenu.getIsDelete());
                throw new BusinessException(ErrorCodeEnum.USER_VERIFY.setParam("数据状态设置错误"));
            }
        }
        if (toUpdate && (StringUtils.isNull(sysMenu.getSysMenuId()) || sysMenuMapper.has(sysMenu.getSysMenuId(), null) == 0)){
            throw new BusinessException(ErrorCodeEnum.DATA_NOT_FOUNT);
        }
    }

    public boolean has(Long sysMenuId,Byte isDelete){
        return sysMenuMapper.has(sysMenuId, isDelete) > 0;
    }
}
